package com.wn.service;

import com.common.base.BaseService;
import com.common.entity.PageParam;
import com.common.entity.Plan;
import com.common.entity.Programme;
import com.common.entity.Rsp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.mapper.PlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
@Service
public class PlanService implements BaseService<Plan> {

    @Resource
    private PlanMapper planMapper;

    @Override
    public PageInfo<Plan> searchSomeByPage(PageParam<Plan> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<Plan> newscategoryList=planMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Plan> newshotelPageInfo = new PageInfo<Plan>(newscategoryList);
            return newshotelPageInfo;
        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<Plan> programmes = planMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Plan> orderPageInfo = new PageInfo<Plan>(programmes);
            return orderPageInfo;
        }

    }

    @Override
    public Plan getOneById(String id) {
        Plan oneById = planMapper.getOneById(id);
        if (!StringUtils.isEmpty(oneById)) {
            return oneById;
        }
        return null;
    }

    @Override
    public Rsp addOne(Plan plan) {
        int i = planMapper.addOne(plan);
        if(i>0){
            return Rsp.success(plan) ;
        }
        return Rsp.fail();
    }

    @Override
    public Rsp update(Plan plan) {
        int i = planMapper.update(plan);
        if(i>0){
            return Rsp.success(planMapper.getOneById(plan.getId())) ;
        }
        return Rsp.fail();
    }

    @Override
    public Rsp deleteLogical(Plan plan) {
        plan.setDeleted(1);
        int i = planMapper.update(plan);
        if(i>0){
            return Rsp.success(planMapper.getOneById(plan.getId())) ;
        }
        return Rsp.fail();
    }
}
